package com.yun.many.auth.controller;

import com.yun.many.spec.exception.BusinessException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindingResult;

import java.util.stream.Collectors;

public class BaseController {

    public void validate(BindingResult result) {
        boolean hasErrors = result.hasErrors();
        if (!hasErrors) {
            return;
        }

        String errorMsg = result.getAllErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.joining(","));

        throw new BusinessException("-1", errorMsg);
    }
}
